package com.example.spinning.controller;

import com.example.spinning.bean.MaterialQuality;
import com.example.spinning.bean.RespBean;
import com.example.spinning.service.MaterialQualityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/material")
public class MaterialQualityController {
    @Autowired
    MaterialQualityService materialQualityService;


    //增加记录
    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public RespBean addNewMaterialQuality( MaterialQuality materialQuality) {
        int result = materialQualityService.addNewMaterialQuality(materialQuality);
        if (result == 1) {
            return new RespBean("success",  "原料信息添加成功");
        } else {
            return new RespBean("error","添加原料信息失败");
        }
    }

    //根据id删除记录
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.DELETE)
    public RespBean deleteById(@PathVariable Long[] ids) {
        boolean result = materialQualityService.deleteMaterialQualityById(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }


    @RequestMapping(value = "/getByMix/", method = RequestMethod.GET)
    public  Map<String, Object> findPage(  Integer page, Integer size, String batch_no, String name, Date date, String origin, String colour){
        PageInfo<MaterialQuality> pageInfor = materialQualityService.findPage(page, size, batch_no, name, date, origin, colour);
             Map<String, Object> map = new HashMap<>();
             map.put("totleCount",pageInfor.getTotal());
             map.put("list",pageInfor.getList());
             return map;


    }
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public RespBean updateById(MaterialQuality materialQuality){
        int result = materialQualityService.updateCategoryById(materialQuality);
        if (result == 1) {
            return new RespBean("success",  "原料信息修改成功");
        } else {
            return new RespBean("error","原料信息修改失败");
        }
    }

}
